package com.tc.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tc.dao.DictionaryMapper;
import com.tc.pojo.Dictionary;
import com.tc.service.DictionaryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/****
 * @Author:admin
 * @Description:Dictionary业务层接口实现类
 * @Date 2019/6/14 0:16
 *****/
@Service
public class DictionaryServiceImpl implements DictionaryService {

    @Autowired
    private DictionaryMapper dictionaryMapper;


    /**
     * Dictionary条件+分页查询
     * @param dictionary 查询条件
     * @param page 页码
     * @param size 页大小
     * @return 分页结果
     */
    @Override
    public PageInfo<Dictionary> findPage(Dictionary dictionary, int page, int size){
        //分页
        PageHelper.startPage(page,size);
        //搜索条件构建
        Example example = createExample(dictionary);
        //执行搜索
        return new PageInfo<Dictionary>(dictionaryMapper.selectByExample(example));
    }

    /**
     * Dictionary分页查询
     * @param page
     * @param size
     * @return
     */
    @Override
    public PageInfo<Dictionary> findPage(int page, int size){
        //静态分页
        PageHelper.startPage(page,size);
        //分页查询
        return new PageInfo<Dictionary>(dictionaryMapper.selectAll());
    }

    /**
     * Dictionary条件查询
     * @param dictionary
     * @return
     */
    @Override
    public List<Dictionary> findList(Dictionary dictionary){
        //构建查询条件
        Example example = createExample(dictionary);
        //根据构建的条件查询数据
        return dictionaryMapper.selectByExample(example);
    }


    /**
     * Dictionary构建查询对象
     * @param dictionary
     * @return
     */
    public Example createExample(Dictionary dictionary){
        Example example=new Example(Dictionary.class);
        Example.Criteria criteria = example.createCriteria();
        if(dictionary!=null){
            // 编号自增长
            if(!StringUtils.isEmpty(dictionary.getId())){
                    criteria.andEqualTo("id",dictionary.getId());
            }
            // 二级编号
            if(!StringUtils.isEmpty(dictionary.getPid())){
                    criteria.andEqualTo("pid",dictionary.getPid());
            }
            // 字段
            if(!StringUtils.isEmpty(dictionary.getText())){
                    criteria.andEqualTo("text",dictionary.getText());
            }
            // 等级
            if(!StringUtils.isEmpty(dictionary.getLev())){
                    criteria.andEqualTo("lev",dictionary.getLev());
            }
            // 是否启用 0启用 1关闭
            if(!StringUtils.isEmpty(dictionary.getIsdisable())){
                    criteria.andEqualTo("isdisable",dictionary.getIsdisable());
            }
        }
        return example;
    }

    /**
     * 删除
     * @param id
     */
    @Override
    public void delete(Integer id){
        dictionaryMapper.deleteByPrimaryKey(id);
    }

    /**
     * 修改Dictionary
     * @param dictionary
     */
    @Override
    public void update(Dictionary dictionary){
        dictionaryMapper.updateByPrimaryKey(dictionary);
    }

    /**
     * 增加Dictionary
     * @param dictionary
     */
    @Override
    public void add(Dictionary dictionary){
        dictionaryMapper.insert(dictionary);
    }

    /**
     * 根据ID查询Dictionary
     * @param id
     * @return
     */
    @Override
    public Dictionary findById(Integer id){
        return  dictionaryMapper.selectByPrimaryKey(id);
    }

    /**
     * 查询Dictionary全部数据
     * @return
     */
    @Override
    public List<Dictionary> findAll() {
        return dictionaryMapper.selectAll();
    }
}
